package ${package};

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cs.core.util.AssertUtil;
import com.cs.web.common.R;
import com.cs.web.entity.${property};
import com.cs.web.entity.dto.${property}SearchDTO;
import com.cs.web.service.I${property}Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * ${(remarks=='')?string(name, remarks)} 接口定义
 * @author ${author}
 * @date ${.now?string("yyyy/MM/dd HH:mm:ss")}
 * @version V1.0
 */
@RestController
@RequestMapping("/api/${alias}")
public class ${property}Controller {

    private final Logger logger = LoggerFactory.getLogger(${property}Controller.class);

    @Autowired
    private I${property}Service ${alias}Service;

    /**
     * 添加记录接口
     * @param r
     * @return
     */
    @PostMapping("/")
    public R save(@RequestBody ${property} r) {
        logger.info("${property}Controller.save param: r is {}", r);
        boolean flag = ${alias}Service.save(r);
		<#if primary??>
        return R.of(flag, r.getId());
		<#else>
		return R.of(flag);
		</#if>
    }


    <#if primary??>
    /**
     * 修改记录接口
     * @param r
     * @return
     */
    @PutMapping("/")
    public R update(@RequestBody ${property} r) {
        logger.info("${property}Controller.update param: r is {}", r);
        AssertUtil.notNull(r.getId(), "id is null");
        boolean flag = ${alias}Service.updateById(r);
        return R.of(flag);
    }
    </#if>
	
	<#if primary??>
    /**
     * 删除记录接口
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R delete(@PathVariable("id") Long id) {
        logger.info("${property}Controller.delete param: id is {}", id);
        boolean flag = ${alias}Service.removeById(id);
        return R.of(flag);
    }
	<#else>
	/**
     * 删除记录接口
     * @param r
     * @return
     */
    @DeleteMapping("/")
    public R delete(@RequestBody ${property} r) {
        logger.info("${property}Controller.delete param: r is {}", r);
		Wrapper<${property}> queryWrapper = new QueryWrapper<>(r);
        boolean flag = ${alias}Service.remove(queryWrapper);
        return R.of(flag);
    }
	</#if>

	<#if primary??>
    /**
     * 单条记录查询接口
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R get(@PathVariable("id") Long id) {
        logger.info("${property}Controller.get param: id is {}", id);
        ${property} entity = ${alias}Service.getById(id);
        return R.ok(entity);
    }
	</#if>

    /**
     * 批量查询记录接口
     * @param r
     * @return
     */
    @PostMapping("/list")
    public R list(@RequestBody ${property} r) {
        logger.info("${property}Controller.list param: r is {}", r);
        Wrapper<${property}> queryWrapper = new QueryWrapper<>(r);
        List<${property}> list = ${alias}Service.list(queryWrapper);
        return R.ok(list);
    }

    /**
     * 分页查询记录接口
     * @param r
     * @return
     */
    @PostMapping("/page/list")
    public R pageList(@RequestBody ${property} r){
        logger.info("${property}Controller.pageList param: r is {}", r);
        Wrapper<${property}> queryWrapper = new QueryWrapper<>(r);
        Page<${property}> page = Page.of(r.getPage(), r.getSize());
        ${alias}Service.page(page, queryWrapper);
        return R.ok(page);
    }
}
